package com.iwhalecloud.bss.kite.cucc.web.controller.customerOrderInformation;

import com.github.pagehelper.PageInfo;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.customerOrderInformation.ICustomerOrderInfoService;
import com.iwhalecloud.bss.kite.cucc.client.api.reception.ICustReceptionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.CustOrderInfoRegion;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.CustomerOrderInformationReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.customerOrderInformation.OrderRejectionCust;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.KiteCustomerInfoReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDTO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.ztesoft.bss.common.util.ContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/customerOrderInformation")
@Api(tags = {"/customerOrderInformation - 集团客户订购信息查询"})
public class CustomerOrderInformationController {

    @Autowired
    private ICustomerOrderInfoService customerOrderInfoService;

    @Autowired
    private ICustReceptionService custReceptionService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @ApiOperation(value = "查询客户列表", notes = "查询客户列表")
    @PostMapping("/selectCustomerOrderInformation")
    public DataResult<PageInfo<CuccCustInfoDTO>> selectCustomerOrderInformation(
        @RequestBody CustomerOrderInformationReq customerOrderInformationReq) {

        PageInfo<CuccCustInfoDTO> customerOrderInfoDTOPageInfo = this.customerOrderInfoService
            .selectCustomerOrderInformation(customerOrderInformationReq);
        return ErrorConsts.SUCCESS.getResult(customerOrderInfoDTOPageInfo);
    }

    @ApiOperation(value = "根据客户id查询用户", notes = "根据客户id查询用户")
    @GetMapping("/selectUserInformationByCustId")
    public DataResult<Map<String, Object>> selectUserInformationByCustId(
        @RequestParam(required = false) String custId, @RequestParam(required = false) String accNum,
        @RequestParam(required = false) String memAccNum, @RequestParam(required = false) String userDiffCode,
        @RequestParam String isSelfAccept, @RequestParam(required = false) Integer pageNum,
        @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) String eparchyCode,
        @RequestParam(required = false) String provinceCode, @RequestParam(required = false) String certTypeCode,
        @RequestParam(required = false) String certCode) {
        if (KiteStringUtils.isEmpty(provinceCode)) {
            provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        }
        if (KiteStringUtils.isEmpty(eparchyCode)) {
            eparchyCode = ContextUtil.getLoginInfo().getUserInfo().getPostLanId();
        }
        pageNum = Objects.isNull(pageNum) ? 1 : pageNum;
        pageSize = Objects.isNull(pageSize) ? 10 : pageSize;
        if (!KiteStringUtils.isEmpty(certTypeCode) && !KiteStringUtils.isEmpty(certCode)) {
            KiteCustomerInfoReq kiteCustomerInfoReq = new KiteCustomerInfoReq();
            kiteCustomerInfoReq.setPageNum(pageNum);
            kiteCustomerInfoReq.setPageSize(pageSize);
            if(KiteStringUtils.isEmpty(kiteCustomerInfoReq.getProvinceCode())){
                kiteCustomerInfoReq.setProvinceCode(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
            }
            if(KiteStringUtils.isEmpty(kiteCustomerInfoReq.getEparchyCode())){
                kiteCustomerInfoReq.setEparchyCode(ContextUtil.getLoginInfo().getUserInfo().getPostLanId());
            }
            kiteCustomerInfoReq.setCertCode(certCode);
            kiteCustomerInfoReq.setCertTypeCode(certTypeCode);
            if(!KiteStringUtils.isEmpty(custId)){
                kiteCustomerInfoReq.setCustId(custId);
            }
            PageInfo<CuccCustInfoDTO> cuccCustInfoDTOPageInfo = custReceptionService.customerListQueryNew(kiteCustomerInfoReq);
            List<CuccCustInfoDTO> CuccCustInfoDTOs = cuccCustInfoDTOPageInfo.getList();
            if(KiteListUtils.isNotEmpty(CuccCustInfoDTOs)){
                CuccCustInfoDTO cuccCustInfoDTO = CuccCustInfoDTOs.get(0);
                String certCustId = cuccCustInfoDTO.getCustId();
                custId = certCustId;
            }
        }
        Map<String, Object> user = this.customerOrderInfoService
            .selectUserInfomationByCustId(custId, accNum, memAccNum, userDiffCode, isSelfAccept, pageNum, pageSize,
                eparchyCode, provinceCode);
        return ErrorConsts.SUCCESS.getResult(user);
    }

    @ApiOperation(value = "获取查询条件中归属地省份", notes = "获取查询条件中归属地省份")
    @GetMapping("/province")
    public DataResult<CustOrderInfoRegion> getProvince() {
        return ErrorConsts.SUCCESS.getResult(customerOrderInfoService.getProvince());
    }

    @ApiOperation(value = "获取查询条件中归属地地市", notes = "获取查询条件中归属地地市")
    @ApiImplicitParams({@ApiImplicitParam(name = "provinceCode", value = "省份编码", required = true)})
    @GetMapping("/city")
    public DataResult<CustOrderInfoRegion> getCity(@RequestParam String provinceCode) {
        return ErrorConsts.SUCCESS.getResult(customerOrderInfoService.getCity(provinceCode));
    }

    @ApiOperation(value = "获取甩单客户编码和名称", notes = "获取甩单客户编码和名称")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "甩单订单编码", required = true)})
    @GetMapping("/getOrderRejectionCust")
    public DataResult<OrderRejectionCust> getOrderRejectionCust(@RequestParam String orderId) {
        return ErrorConsts.SUCCESS.getResult(customerOrderInfoService.getOrderRejectionCust(orderId));
    }
}
